package org.jeecg.modules.safeProduction.controller;

import java.text.SimpleDateFormat;
import java.time.LocalDate;
import java.time.ZoneId;
import java.time.temporal.ChronoUnit;
import java.util.Arrays;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.system.query.QueryGenerator;
import org.jeecg.common.util.oConvertUtils;
import org.jeecg.modules.employee.entity.Employee;
import org.jeecg.modules.employee.service.IEmployeeService;
import org.jeecg.modules.safeProduction.entity.SafeProduction;
import org.jeecg.modules.safeProduction.service.ISafeProductionService;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.extern.slf4j.Slf4j;

import org.jeecgframework.poi.excel.ExcelImportUtil;
import org.jeecgframework.poi.excel.def.NormalExcelConstants;
import org.jeecgframework.poi.excel.entity.ExportParams;
import org.jeecgframework.poi.excel.entity.ImportParams;
import org.jeecgframework.poi.excel.view.JeecgEntityExcelView;
import org.jeecg.common.system.base.controller.JeecgController;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;
import org.springframework.web.servlet.ModelAndView;
import com.alibaba.fastjson.JSON;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.jeecg.common.aspect.annotation.AutoLog;

import static org.checkerframework.checker.units.qual.Prefix.one;

/**
 * @Description: 安全生产
 * @Author: jeecg-boot
 * @Date:   2025-02-11
 * @Version: V1.0
 */
@Api(tags="安全生产")
@RestController
@RequestMapping("/safeProduction/safeProduction")
@Slf4j
public class SafeProductionController extends JeecgController<SafeProduction, ISafeProductionService> {
	@Autowired
	private ISafeProductionService safeProductionService;
	@Autowired
	private IEmployeeService employeeService;
	
	/**
	 * 分页列表查询
	 *
	 * @param safeProduction
	 * @param pageNo
	 * @param pageSize
	 * @param req
	 * @return
	 */
	@ApiOperation(value="安全生产-分页列表查询", notes="安全生产-分页列表查询")
	@GetMapping(value = "/list")
	public Result<?> queryPageList(SafeProduction safeProduction,
								   @RequestParam(name="pageNo", defaultValue="1") Integer pageNo,
								   @RequestParam(name="pageSize", defaultValue="10") Integer pageSize,
								   HttpServletRequest req) {
		QueryWrapper<SafeProduction> queryWrapper = QueryGenerator.initQueryWrapper(safeProduction, req.getParameterMap());
		Page<SafeProduction> page = new Page<SafeProduction>(pageNo, pageSize);
		IPage<SafeProduction> pageList = safeProductionService.page(page, queryWrapper);
		return Result.OK(pageList);
	}

	 @ApiOperation(value="安全生产-获取大屏信息", notes="安全生产-获取大屏信息")
	 @GetMapping(value = "/m/getData")
	 public Result<?> getData() {
		 LambdaQueryWrapper<SafeProduction> wrapper = Wrappers.lambdaQuery();
		 wrapper.orderByDesc(SafeProduction::getCreateTime);
		 wrapper.last("limit 1");
		 Map<String, Object> map = safeProductionService.getMap(wrapper);
		 if (map == null) {
			 return Result.OK();
		 } else {
			 String safeDate = String.valueOf(map.get("safe_date"));
			 String nightManager = String.valueOf(map.get("night_manager"));
			 LambdaQueryWrapper<Employee> employeeWrapper = Wrappers.lambdaQuery();
			 employeeWrapper.eq(Employee::getName, nightManager);
			 List<Employee> list = employeeService.list(employeeWrapper);
			 if (!list.isEmpty()) {
				 map.put("face_url", list.get(0).getFaceUrl());
				 map.put("phone", list.get(0).getPhone());
			 }
			 try {
				 // 将字符串解析为Date对象
				 SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
				 Date date = sdf.parse(safeDate);
				 // 将Date对象转换为LocalDate
				 LocalDate localDate = date.toInstant().atZone(ZoneId.systemDefault()).toLocalDate();
				 // 获取今天的日期
				 LocalDate today = LocalDate.now();
				 // 计算两个日期之间的天数差
				 long daysBetween = ChronoUnit.DAYS.between(localDate, today);
				 map.put("safeDate", daysBetween);
				 return Result.OK(map);
			 } catch (Exception e) {
				 log.error(e.getMessage(), e);
				 return Result.OK(map);
			 }
		 }
	 }

	 @ApiOperation(value="安全生产-获取维护信息", notes="安全生产-获取维护信息")
	 @GetMapping(value = "/getOne")
	 public Result<?> getOne() {
		 LambdaQueryWrapper<SafeProduction> wrapper = Wrappers.lambdaQuery();
		 wrapper.orderByDesc(SafeProduction::getCreateTime);
		 wrapper.last("limit 1");
		 SafeProduction one = safeProductionService.getOne(wrapper);
		 if (one == null) {
			 SafeProduction safeProduction = new SafeProduction();
			 safeProduction.setSafeDate(new Date());
			 safeProduction.setStartDate(new Date());
			 safeProduction.setEndDate(new Date());
			 safeProductionService.save(safeProduction);
			 return Result.OK(safeProduction);
		 }
		 return Result.OK(one);
	 }
	
	/**
	 *   添加
	 *
	 * @param safeProduction
	 * @return
	 */
	@AutoLog(value = "安全生产-添加")
	@ApiOperation(value="安全生产-添加", notes="安全生产-添加")
	@PostMapping(value = "/add")
	public Result<?> add(@RequestBody SafeProduction safeProduction) {
		safeProductionService.save(safeProduction);
		return Result.OK("添加成功！");
	}
	
	/**
	 *  编辑
	 *
	 * @param safeProduction
	 * @return
	 */
	@AutoLog(value = "安全生产-编辑")
	@ApiOperation(value="安全生产-编辑", notes="安全生产-编辑")
	@PutMapping(value = "/edit")
	public Result<?> edit(@RequestBody SafeProduction safeProduction) {
		safeProductionService.updateById(safeProduction);
		return Result.OK("编辑成功!");
	}
	
	/**
	 *   通过id删除
	 *
	 * @param id
	 * @return
	 */
	@AutoLog(value = "安全生产-通过id删除")
	@ApiOperation(value="安全生产-通过id删除", notes="安全生产-通过id删除")
	@DeleteMapping(value = "/delete")
	public Result<?> delete(@RequestParam(name="id",required=true) String id) {
		safeProductionService.removeById(id);
		return Result.OK("删除成功!");
	}
	
	/**
	 *  批量删除
	 *
	 * @param ids
	 * @return
	 */
	@AutoLog(value = "安全生产-批量删除")
	@ApiOperation(value="安全生产-批量删除", notes="安全生产-批量删除")
	@DeleteMapping(value = "/deleteBatch")
	public Result<?> deleteBatch(@RequestParam(name="ids",required=true) String ids) {
		this.safeProductionService.removeByIds(Arrays.asList(ids.split(",")));
		return Result.OK("批量删除成功!");
	}
	
	/**
	 * 通过id查询
	 *
	 * @param id
	 * @return
	 */
	@AutoLog(value = "安全生产-通过id查询")
	@ApiOperation(value="安全生产-通过id查询", notes="安全生产-通过id查询")
	@GetMapping(value = "/queryById")
	public Result<?> queryById(@RequestParam(name="id",required=true) String id) {
		SafeProduction safeProduction = safeProductionService.getById(id);
		if(safeProduction==null) {
			return Result.error("未找到对应数据");
		}
		return Result.OK(safeProduction);
	}

    /**
    * 导出excel
    *
    * @param request
    * @param safeProduction
    */
    @RequestMapping(value = "/exportXls")
    public ModelAndView exportXls(HttpServletRequest request, SafeProduction safeProduction) {
        return super.exportXls(request, safeProduction, SafeProduction.class, "安全生产");
    }

    /**
      * 通过excel导入数据
    *
    * @param request
    * @param response
    * @return
    */
    @RequestMapping(value = "/importExcel", method = RequestMethod.POST)
    public Result<?> importExcel(HttpServletRequest request, HttpServletResponse response) {
        return super.importExcel(request, response, SafeProduction.class);
    }

}
